﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using Axis.Web.Administrator.Global;
using Axis.Dao.Core;
using Axis.BusinessObject.Core;
using System.Xml;
using Axis.BusinessObject.Research;
using Axis.Dao.Research;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Axis.Web.Administrator.Views.Accounts.Service
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    [System.Web.Script.Services.ScriptService]
    public class MemberService2 : System.Web.Services.WebService
    {

        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public CApplicationMessage VerifyMenber(int ID, string Email, bool IsReset, string Action, string Active)
        {
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(HttpContext.Current);

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            root.SetAttribute("Action", Action);
            root.SetAttribute("ID", ID.ToString());
            root.SetAttribute("Email", Email);
            root.SetAttribute("Active", Active);
            CCoreDao core = new CCoreDao();
            return core.ActionMemberList(doc.OuterXml);
        }

        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetListData(string Action, string FunctionCode)
        {
            CCore cCore = new CCore();
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            if (CSecurity.USER_ID == 0)
                return "USERID:" + Session["USERID"].ToString();
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            root.SetAttribute("Action", Action);
            root.SetAttribute("FunctionCode", FunctionCode);
            CCoreDao core = new CCoreDao();
            IList<CListData> list = core.GetListData(root.OuterXml);
            string output = new JavaScriptSerializer().Serialize(list);
            return output;
        }
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetMemberGiftData(string ID)
        {
            CCore cCore = new CCore();
            XmlDocument doc = new XmlDocument();
            if (CSecurity.USER_ID == 0)
                return "USERID:" + Session["USERID"].ToString();
            IList<CGift> list = GetMemberGift(ID, CSecurity.USER_ID.ToString());// 
            IsoDateTimeConverter isoDateTimeConverter = new IsoDateTimeConverter();
            isoDateTimeConverter.DateTimeFormat = "MM/dd/yyyy HH:mm";
            string output = JsonConvert.SerializeObject(list,isoDateTimeConverter);            
            return output;
        }

        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetGiftByCondition(string ProjectID, string GiftID, string GiftReveiced, string FromDate, string ToDate, string FromMark, string ToMark)
        {
            CCore cCore = new CCore();
            XmlDocument doc = new XmlDocument();
            if (CSecurity.USER_ID == 0)
                return "USERID:" + Session["USERID"].ToString();
            IList<CMemberList> list = GetGiftSearch(CSecurity.USER_ID.ToString(), ProjectID, GiftID, GiftReveiced, FromDate, ToDate, FromMark, ToMark);// 
            IsoDateTimeConverter isoDateTimeConverter = new IsoDateTimeConverter();
            isoDateTimeConverter.DateTimeFormat = "MM/dd/yyyy HH:mm";

            
            string output = JsonConvert.SerializeObject(list, isoDateTimeConverter);
            return output;
        }


        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetMemberInfo(string ID)
        {
            CCoreDao core = new CCoreDao();
            XmlDocument doc = new XmlDocument();
            if (CSecurity.USER_ID == 0)
                return "USERID:" + Session["USERID"].ToString();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", Session["USERID"].ToString());
            root.SetAttribute("MemberID", ID);
            root.SetAttribute("ID", ID);
            root.SetAttribute("SecrectToken", "");
            root.SetAttribute("Version", "2");
            string inputValue = root.OuterXml;
            IList<CMemberList> list = core.GetMemberList(inputValue);
            string output = new JavaScriptSerializer().Serialize(list);
            return output;
        }

        public IList<CGift> GetMemberGift(string menberID, string userID)
        {
            var member = new CMemberList();
            CResearchDao cResearchDao = new CResearchDao();

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", userID);
            root.SetAttribute("MemberID",menberID);
            root.SetAttribute("SecrectToken", "");
            root.SetAttribute("Context", "MemberAdmin");
            string inputValue = root.OuterXml;
            IList<CGift> list = cResearchDao.GetGift(inputValue);
            return list;
        }


        public IList<CMemberList> GetGiftSearch(string userID, string projectID, string giftID, string giftReceiced, string FromDate, string ToDate, string FromMark, string ToMark)
        {
            var member = new CMemberList();
            CCoreDao coreDao = new CCoreDao();
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", userID);
            root.SetAttribute("ProjectID", projectID);
            root.SetAttribute("GiftID", giftID);
            root.SetAttribute("GiftReveiced", giftReceiced);
            root.SetAttribute("SecrectToken", "");
            root.SetAttribute("Context", "Gift");
            if (FromDate.Length == 10)
            {
                string temp = string.Format("{0}-{1}-{2}", FromDate.Substring(6, 4), FromDate.Substring(3, 2), FromDate.Substring(0, 2));
                FromDate = temp;
            }
            else
                FromDate = "";
            if (ToDate.Length == 10)
            {
                string temp2 = string.Format("{0}-{1}-{2}", ToDate.Substring(6, 4), ToDate.Substring(3, 2), ToDate.Substring(0, 2));
                ToDate = temp2;
            }
            else
                ToDate = "";

            root.SetAttribute("FromDate", FromDate);
            root.SetAttribute("ToDate", ToDate);
            root.SetAttribute("FromMark", FromMark);
            root.SetAttribute("ToMark", ToMark);

            string inputValue = root.OuterXml;
            IList<CMemberList> list = coreDao.GetMemberList(inputValue);
            return list;
        }
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string UpdateGiftData(string MemberGiftID, string ActionMemberID, string ReceiptQuantity, string ReceiptDateTime, string ReceiptNote)
        {
            CCore cCore = new CCore();
            //<InputValue ID="1" UserID="1" ActionMemberID="1" ReceiptDateTime="2013-11-29 12:50:25" ReceiptNote="Qua nhan tai van phong" ReceiptQuantity="1" Action="RECEIPT"/>

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            root.SetAttribute("Action", "RECEIPT");
            root.SetAttribute("ID", MemberGiftID);
            root.SetAttribute("ReceiptQuantity", ReceiptQuantity);
            if (ReceiptDateTime.Length == 10)
            {
                string temp = string.Format("{0}-{1}-{2}", ReceiptDateTime.Substring(6, 4), ReceiptDateTime.Substring(3, 2), ReceiptDateTime.Substring(0, 2));
                ReceiptDateTime = temp;
            }
            root.SetAttribute("ReceiptDateTime", ReceiptDateTime);
            
            root.SetAttribute("ReceiptNote", ReceiptNote);

            CResearchDao core = new CResearchDao();
            string output = new JavaScriptSerializer().Serialize(core.ActionMemberGift(doc.OuterXml));
            return output;
        }
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetGiftLotteryList(string ProjectID)
        {
            CResearchDao core = new CResearchDao();
            XmlDocument doc = new XmlDocument();
            if (CSecurity.USER_ID == 0)
                return "USERID:" + Session["USERID"].ToString();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", Session["USERID"].ToString());
           // root.SetAttribute("MemberID", Session["USERID"].ToString());
           // root.SetAttribute("ProjectID", ProjectID);
          //  root.SetAttribute("Context", "MemberAdmin");
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN);
            root.SetAttribute("GiftGroup", "50,51");
            string inputValue = root.OuterXml;
            IList<CGift> list = core.GetGift(inputValue);
            string output = new JavaScriptSerializer().Serialize(list);
            return output;
        }

        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetGiftLotteryListByID(string ProjectID)
        {
            CResearchDao core = new CResearchDao();
            XmlDocument doc = new XmlDocument();
            if (CSecurity.USER_ID == 0)
                return "USERID:" + Session["USERID"].ToString();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", Session["USERID"].ToString());
            // root.SetAttribute("MemberID", Session["USERID"].ToString());
             root.SetAttribute("ProjectID", ProjectID);
            //  root.SetAttribute("Context", "MemberAdmin");
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN);
            root.SetAttribute("GiftGroup", "51");
            string inputValue = root.OuterXml;
            IList<CGift> list = core.GetGift(inputValue);
            string output = new JavaScriptSerializer().Serialize(list);
            return output;
        }

        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string UpdateGiftLottory(string ProjectID, string ActionMemberID, string LotteGiftID, string ConvertFromMarkDateTime, string GiftID)
        {
            CCore cCore = new CCore();
            //<InputValue ID="1" UserID="1" ActionMemberID="1" ReceiptDateTime="2013-11-29 12:50:25" ReceiptNote="Qua nhan tai van phong" ReceiptQuantity="1" Action="RECEIPT"/>

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            root.SetAttribute("Action", "CONVERTLOTTETOGIFT");
            root.SetAttribute("ProjectID", ProjectID);
            root.SetAttribute("MemberID", ActionMemberID);
            root.SetAttribute("LotteGiftID", LotteGiftID);
            root.SetAttribute("ConvertFromMarkDateTime", ConvertFromMarkDateTime);
            root.SetAttribute("GiftID", GiftID);

            CResearchDao core = new CResearchDao();
            string output = new JavaScriptSerializer().Serialize(core.ActionMemberGift(doc.OuterXml));
            return output;
        }

    }
}
